package com.yunupmall.service.ford.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yunupmall.api.fordVO.StatisticsCommtityPO;
import com.yunupmall.api.fordentity.Kind;
import com.yunupmall.api.fordentity.Pay;
import com.yunupmall.api.po.OrderCounts;
import com.yunupmall.api.po.UserCounts;
import com.yunupmall.mapper.commoditymapper.StatisticMapper;
import com.yunupmall.service.admin.service.MemberService;
import com.yunupmall.service.ford.service.StatisticSeriver;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class Statisticsimpl implements StatisticSeriver {
    private final StatisticMapper statisticMapper;
    private final MemberService mberService;

    /**
     * 统计商品数据
     *
     * @return
     */
    @Override
    public StatisticsCommtityPO getCommtity() {
        StatisticsCommtityPO statisticsCommtityPO = new StatisticsCommtityPO();
        Integer onCommtityCount = statisticMapper.getOnCommtityCount();
        statisticsCommtityPO.setName("商品总数");
        statisticsCommtityPO.setValue(onCommtityCount);
        Integer inStaticCommtityCount = statisticMapper.getInStaticCommtityCount();
        statisticsCommtityPO.setInName("下架数量");
        statisticsCommtityPO.setInValue(inStaticCommtityCount);
        Integer onStaticCommtityCount = statisticMapper.getOnStaticCommtityCount();
        statisticsCommtityPO.setOnName("上架数量");
        statisticsCommtityPO.setOnValue(onStaticCommtityCount);
        return statisticsCommtityPO;
    }

    /**
     * 用户数据
     *
     * @return
     */
    @Override
    public StatisticsCommtityPO getMemberTic() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //昨天
        Date beginOfDay1 = DateUtil.beginOfDay(new Date());
        Calendar c = Calendar.getInstance();
        c.setTime(beginOfDay1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterBeginDay = c.getTime();
        String a = simpleDateFormat.format(yesterBeginDay);
        //昨天
        Date endOfDay1 = DateUtil.endOfDay(new Date());
        c.setTime(endOfDay1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterEndDay = c.getTime();
        String b = simpleDateFormat.format(yesterEndDay);

        DateTime beginOfDay = DateUtil.beginOfDay(new Date());
        DateTime endOfDay = DateUtil.endOfDay(new Date());
        StatisticsCommtityPO statisticsCommtityPO = new StatisticsCommtityPO();
        Integer memberCount = statisticMapper.getMemberCount();
        statisticsCommtityPO.setName("会员总数");
        statisticsCommtityPO.setValue(memberCount);
        Integer dayMemberCount = statisticMapper.getDayMemberCount(beginOfDay, endOfDay);
        statisticsCommtityPO.setInName("今日注册人数");
        statisticsCommtityPO.setInValue(dayMemberCount);
        Integer day1MemberCount = statisticMapper.getOnDayMemberCount(a, b);
        statisticsCommtityPO.setOnName("昨日注册人数");
        statisticsCommtityPO.setOnValue(day1MemberCount);
        return statisticsCommtityPO;
    }

    /**
     * 订单数据
     *
     * @return
     */
    @Override
    public StatisticsCommtityPO getOrderTic() {
        StatisticsCommtityPO statisticsCommtityPO = new StatisticsCommtityPO();
        //今日订单
        DateTime beginOfDay = DateUtil.beginOfDay(new Date());
        DateTime endOfDay = DateUtil.endOfDay(new Date());
        Integer dayOrderCount = statisticMapper.getDayOrderCount(beginOfDay, endOfDay);
        statisticsCommtityPO.setName("今日订单数");
        statisticsCommtityPO.setValue(dayOrderCount);
        //今日销售额
        List<Pay> dayPay = statisticMapper.getDayPay(beginOfDay, endOfDay);
        double mouny = 0.0;
        for (Pay pay : dayPay) {
            mouny += pay.getTotalAmount();
        }
        statisticsCommtityPO.setInName("今日销售额");
        statisticsCommtityPO.setInAmount(mouny);
        //昨天销售额
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginOfDay1 = DateUtil.beginOfDay(new Date());
        Calendar c = Calendar.getInstance();
        c.setTime(beginOfDay1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterBeginDay = c.getTime();
        String a = simpleDateFormat.format(yesterBeginDay);
        //昨天
        Date endOfDay1 = DateUtil.endOfDay(new Date());
        c.setTime(endOfDay1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterEndDay = c.getTime();
        String b = simpleDateFormat.format(yesterEndDay);
        List<Pay> onDayPay = statisticMapper.getOnDayPay(a, b);
        double OnMouny = 0.0;
        for (Pay pay : onDayPay) {
            OnMouny += pay.getTotalAmount();
        }
        statisticsCommtityPO.setOnName("昨日销售额");
        statisticsCommtityPO.setOnAmount(OnMouny);
        return statisticsCommtityPO;
    }

    /**
     * 订单年度数据
     *
     * @param date
     * @return
     */
    @Override
    public List<OrderCounts> getYearOrderTic(String date) {
        //订单数量
        List<OrderCounts> yearOrderCounts = statisticMapper.getYearOrderCounts(date);
        if (ObjectUtil.isNotEmpty(yearOrderCounts)) {
            for (int i = 1; i <= 12; i++) {
                String month = null;
                String finalMonth = month;
                int finalI = i;
                final Integer[] falg = {null};
                int finalI1 = i;
                //判断是否有未出现的月份，有则填补集合
                yearOrderCounts.forEach(orderCounts -> {
                    if (Objects.equals(orderCounts.getMonth(), finalI1)) {
                        falg[0] = 10;
                    }
                });
                if (ObjectUtil.isEmpty(falg[0])) {
                    OrderCounts orderCounts = new OrderCounts();
                    orderCounts.setMonth(i);
                    orderCounts.setCounts(0);
                    yearOrderCounts.add(orderCounts);
                }
            }
            //销售额
            List<OrderCounts> yearSaleQuota = statisticMapper.getYearSaleQuota(date);
            for (OrderCounts orderCounts : yearSaleQuota) {
                for (OrderCounts yearcounts : yearOrderCounts) {
                    if (Objects.equals(orderCounts.getMonth(),yearcounts.getMonth()) ){
                        yearcounts.setSale(orderCounts.getSale());
                    }
                }
            }
            List<OrderCounts> collect = yearOrderCounts.stream().sorted(Comparator.comparing(OrderCounts::getMonth)).collect(Collectors.toList());
            return collect;
        }else {
            return null;
        }
    }

    /**
     * 类别数据统计
     * @return
     */
    @Override
    public List<OrderCounts> kindCommtityStatistics() {
        List<Kind> kindList = statisticMapper.getKindList();
        List<OrderCounts> orderCountList = new ArrayList<>();
        final OrderCounts[] orderCounts = new OrderCounts[1];
        kindList.forEach(kind -> {
             orderCounts[0] = new OrderCounts();
             orderCounts[0].setName(kind.getName());
            Integer kindCommtityCount = statisticMapper.getKindCommtityCount(kind.getId());
            if (ObjectUtil.isNotEmpty(kindCommtityCount)){
                orderCounts[0].setCounts(kindCommtityCount);
            }
            orderCountList.add(orderCounts[0]);
        });
        return orderCountList;
    }
}
